﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeatherStationSys.Interface
{
    /// <summary>
    /// 抽象的温度传感器
    /// </summary>
    public abstract class AbstractTemperatureSensor : ISubject
    {
        //最后1次读取到的温度
        private double lastReading;

        /// <summary>
        /// 读取温度
        /// </summary>
        /// <returns></returns>
        public abstract double Read();

        /// <summary>
        /// 读数是否发生了变化(注-模板方法模式：读取温度可变的，检查逻辑固定的)
        /// </summary>
        public void Check()
        {
            double temp = Read();
            if (temp != lastReading)
            {
                lastReading = temp;
                //通知观察者
                NotifyObservers(temp);
            }
        }

        /// <summary>
        /// 添加观察者
        /// </summary>
        /// <param name="obj"></param>
        public abstract void AddObserver(IObserver obj);

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="obj"></param>
        public abstract void UnRegisterObserver(IObserver obj);

        /// <summary>
        /// 通知观察者
        /// </summary>
        /// <param name="temperature"></param>
        public abstract void NotifyObservers(double temperature);
    }
}
